<template>
  <div class="wrapper">
    <div class="login-box">
      <div class="logo">
        <img src="../../assets/logo.png" height="120px" alt="">
      </div>
      <!-- <h3 class="title">登录界面</h3> -->
      <div>{{ info }}</div>
      <el-form
        :model="loginForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="60px"
        class="demo-ruleForm"
      >
        <el-form-item label="账号" prop="username">
          <el-input
            type="text"
            v-model="loginForm.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        
          <div @click="submitForm('ruleForm')" class="button">提交<span></span>
			<span></span>
			<span></span>
			<span></span></div>
          <div @click="resetForm('ruleForm')" class="button">重置<span></span>
			<span></span>
			<span></span>
			<span></span></div>
       
      </el-form>
    </div>
  </div>
</template>

<script>
import jwt from 'jwt-decode'
import {mapMutations} from 'vuex'
 export default {
    data() {
      var validateUser = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入账号'));
        } else {
          callback();
        }
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          callback();
        }
      };
      return {
        info:'',
        loginForm: {
          username: '',
          password: '',
        },
        rules: {
          username: [
            { validator: validateUser, trigger: 'blur' }
          ],
          password: [
            { validator: validatePass, trigger: 'blur' }
          ],
        }
      };
    },
    methods: {
      ...mapMutations('loginModule',['setUser']),
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let { username,password} = this.loginForm;
            //请求登录接口------------- 
            this.$api.getLogin({
              username,password
            }).then(res=>{
              console.log('-----',res.data);
              if(res.data.status===200){
                console.log(jwt(res.data.data));
                //登录成功后：1. 存储登录信息  2. 跳转网页 3. 顶部区域显示用户信息  4. 持久化
                let obj ={
                  user:jwt(res.data.data).username,
                  token:res.data.data
                }
                this.setUser(obj)
                //存储本地
                localStorage.setItem('user',JSON.stringify(obj))
                //跳转
                this.$router.push('/')
                // this.info=''

              }else{
                //账号或者密码错误
                // this.info='账号或者密码错误'
                 this.$message.error('错了哦，这是一条错误消息');
              }
            })


          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style lang='less' scoped>
.wrapper{
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
  background-image: linear-gradient(#414141, #304156);
}
.login-box {
  width: 400px;
  height: 260px;
  padding: 20px;
  margin: 0 auto;
  margin-top: 280px;
  width: 400px;
	padding:  40px;
	background: rgba(0,0,0,0.2);
	box-shadow: 0 15px 25px rgba(0,0,0,0.4);
  position: relative;
}
.logo{
  position: absolute;
  width: 120px;
  height: 120px;
  background: #fff;
  border-radius: 50%;
  overflow: hidden;
  top:-80px;
  left:50%;
  margin-left: -60px;
  padding:8px;
}
.demo-ruleForm{
  margin-top: 60px;
}


.button{
  float: left;
  width: 30px;
		overflow: hidden;
		position:relative;
		padding: 10px 20px;
    margin: 10px 10px 0 88px;
		color: #fff;
/*		去下划线*/
		text-decoration: none;
/*		过度*/
		transition: all 0.5s;
    cursor: pointer;
	}
	.button:hover{
		color: #080e16;
		border-radius: 5px;
		background: #fff;
		box-shadow: 0 0 5px #fff,0 0 25px #fff,0 0 50px #fff,0 0 100px #fff;
	}
  .button span{
		position:absolute;
	}
	.button span:first-child{
		top:0;
		left: -100%;
		width: 100%;
		height: 2px;
/*		to right就是往右边*/
		background: linear-gradient(to right,transparent,#fff);
		animation: move1 1s linear infinite;
	}
	.button span:nth-child(2){
		right: 0;
		top:-100%;
		height: 100%;
		width: 2px;
		background: linear-gradient(transparent,#fff);
		animation: move2 1s linear 0.25s infinite;
	}
	.button span:nth-child(3){
		bottom: 0;
		right:-100%;
		width: 100%;
		height: 2px;
		background: linear-gradient(to left,transparent,#fff);
		animation: move3 1s linear 0.5s infinite;
	}
	.button span:last-child{
		left: 0;
		bottom:-100%;
		height: 100%;
		width: 2px;
		background: linear-gradient(#fff,transparent);
		animation: move4 1s linear 0.75s infinite;
	}
/*	动画*/
	@keyframes move1{
		0%{
			left: -100%;
		}
		50%,100%{
			left: 100%;
		}
	}
	@keyframes move2{
		0%{
			top: -100%;
		}
		50%,100%{
			top: 100%;
		}
	}
	@keyframes move3{
		0%{
			right: -100%;
		}
		50%,100%{
			right: 100%;
		}
	}
	@keyframes move4{
		0%{
			bottom: -100%;
		}
		50%,100%{
			bottom: 100%;
		}
	}

</style>